﻿
using CSharpLibs.Annotations;

namespace System.IO
{
	[Builtin]
	public class DirectoryInfo : FileSystemInfo
	{
		public DirectoryInfo(string s)
			: base(s)
		{ ;}

		public void Create()
		{ Directory.CreateDirectory(base.FullPath); }

		public override void Delete()
		{ Directory.Delete(base.FullPath); }

		public DirectoryInfo[] GetDirectories()
		{
			string[] sd = Directory.GetDirectories(base.FullPath);

			DirectoryInfo[] res = new DirectoryInfo[sd.Length];
			for(int i = 0; i < sd.Length; ++i)
				res[i] = new DirectoryInfo(sd[i]);
			return res;
		}

		public DirectoryInfo[] GetDirectories(string match)
		{
			string[] sd = Directory.GetDirectories(base.FullPath, match);

			DirectoryInfo[] res = new DirectoryInfo[sd.Length];
			for(int i = 0; i < sd.Length; ++i)
				res[i] = new DirectoryInfo(sd[i]);
			return res;
		}

		public DirectoryInfo[] GetDirectories(string match, SearchOption opt)
		{
			string[] sd = Directory.GetDirectories(base.FullPath, match, opt);

			DirectoryInfo[] res = new DirectoryInfo[sd.Length];
			for(int i = 0; i < sd.Length; ++i)
				res[i] = new DirectoryInfo(sd[i]);
			return res;
		}

		public FileInfo[] GetFiles()
		{
			string[] sd = Directory.GetFiles(base.FullPath);

			FileInfo[] res = new FileInfo[sd.Length];
			for(int i = 0; i < sd.Length; ++i)
				res[i] = new FileInfo(sd[i]);
			return res;
		}

		public FileInfo[] GetFiles(string match)
		{
			string[] sd = Directory.GetFiles(base.FullPath, match);

			FileInfo[] res = new FileInfo[sd.Length];
			for(int i = 0; i < sd.Length; ++i)
				res[i] = new FileInfo(sd[i]);
			return res;
		}

		public FileInfo[] GetFiles(string match, SearchOption opt)
		{
			string[] sd = Directory.GetFiles(base.FullPath, match, opt);

			FileInfo[] res = new FileInfo[sd.Length];
			for(int i = 0; i < sd.Length; ++i)
				res[i] = new FileInfo(sd[i]);
			return res;
		}

		private static string GetDirName(string fullPath)
		{
			if(fullPath.Length > 3)
			{
				String path = fullPath;
				if(fullPath.EndsWith(Path.DirectorySeparatorString))
				{
					path = fullPath.Substring(0, fullPath.Length - 1);
				}
				return Path.GetFileName(path);
			}
			return fullPath;
		}
	
		public void MoveTo(string destDirName)
		{ Directory.Move(base.FullPath, destDirName); }

		public override bool Exists
		{ get { return Directory.Exists(base.FullPath); } }

		public override string FullName
		{ get { return base.FullPath; } }

		public override string Name
		{ get { return GetDirName(base.FullPath); } } 
	}
}
